local shichou = fk.CreateSkill({
  name = "kl__shichou",
})

local HX = require "packages/konglong/hxfs2"

shichou:addEffect(fk.EventPhaseStart, {
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(shichou.name) and player.phase == Player.Start and
    table.find(player.room.alive_players, function(p) return p ~= player end)
  end,
  on_cost = function (self, event, target, player, data)
    local targets = table.filter(player.room.alive_players, function(p) return p ~= player end)
    local tos = player.room:askToChoosePlayers(player,{
      targets = targets,
      min_num = 1,
      max_num = 1,
      skill_name = self.name,
      cancelable = true,
      prompt = "#kl__shichou-choose",
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:loseHp(player, 1, shichou.name)
    room:addPlayerMark(to, "@@kl__shichou-turn", 1)
    room:addPlayerMark(to, MarkEnum.UncompulsoryInvalidity .. "-turn")
  end,
})

shichou:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(shichou.name) and table.find(data.tos, function(p) return p:getMark("@@kl__shichou-turn") > 0 end) and data.card.is_damage_card
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data.disresponsiveList = data.disresponsiveList or {}
    for _, p in ipairs(player.room.alive_players) do
      if p:getMark("@@kl__shichou-turn") > 0 then
        table.insertIfNeed(data.disresponsiveList, p)
      end
    end
  end,
})

shichou:addEffect(fk.EventPhaseStart, {
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(shichou.name) and player.phase == Player.Finish and player:usedSkillTimes(shichou.name) > 0
  end,
  on_cost = function (self, event, target, player, data)
    local choice = player.room:askToChoice(player,{
      choices = {"kl__shichou-1","kl__shichou-2"},
      skill_name = shichou.name,
    })
    if choice then
      event:setCostData(self, {choice = choice})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    if choice == "kl__shichou-1" then
      room:changeShield(player, 1)
    elseif choice == "kl__shichou-2" then
      HX.DrawCardFromPile(room, player, shichou.name, 1)
    end
  end,
})


return shichou
